package com.forgenet.testclient.panel;

import com.forgenet.testclient.ClientMain;
import com.sparrow.common.util.PropertiesUtil;
import com.sparrow.common.util.SignUtil;
import com.sparrow.common.util.TimeUtil;
import com.sparrow.example_message.base.req.ReqLoginAuthPacket;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginGamePanel extends JPanel implements ActionListener {

    private final JTextField ipField;
    private final JTextField portField;
    private final JTextField serverIdField;
    private final JTextField usernameField;
    private final JButton loginButton;

    public LoginGamePanel() {
        setLayout(new GridBagLayout());

        int row = 0;

        JLabel ipLabel = new JLabel("服务器 IP:");
        add(ipLabel, createGBC(0, row));
        ipField = new JTextField("127.0.0.1", 20);
        add(ipField, createGBC(1, row++));

        JLabel portLabel = new JLabel("端口:");
        add(portLabel, createGBC(0, row));
        portField = new JTextField("9001", 20);
        add(portField, createGBC(1, row++));

        JLabel serverIdLabel = new JLabel("服务器 ID:");
        add(serverIdLabel, createGBC(0, row));
        serverIdField = new JTextField("1", 20);
        add(serverIdField, createGBC(1, row++));

        JLabel usernameLabel = new JLabel("账号:");
        add(usernameLabel, createGBC(0, row));
        usernameField = new JTextField(20);
        add(usernameField, createGBC(1, row++));

        loginButton = new JButton("登录验证");
        loginButton.addActionListener(this);
        add(loginButton, createGBC(1, row));
    }

    private GridBagConstraints createGBC(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        return gbc;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String username = usernameField.getText();
        String ip = ipField.getText();
        int port = Integer.parseInt(portField.getText());
        String serverId = serverIdField.getText();

        try {
            // 连接服务器（如果你用 Netty，则需要这样连接）
//            if (ClientMain.channel == null || !ClientMain.channel.isActive()) {
//                Bootstrap bootstrap = ClientMain.createBootstrap(); // 你需要实现这个方法返回 Bootstrap
//                ChannelFuture future = bootstrap.connect(ip, port).sync();
//                ClientMain.channel = future.channel();
//            }

            ReqLoginAuthPacket req = new ReqLoginAuthPacket();
            req.setAccount(username);
            req.setTime(TimeUtil.timestamp());

            req.setSign(SignUtil.generateSign(req.getAccount() + "_" + req.getTime(), PropertiesUtil.getProperty("app.key")));

            // 如果协议中支持服务器ID，你可以加到 req 里
            // req.setServerId(Integer.parseInt(serverId));

            ClientMain.channel.writeAndFlush(req);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "连接服务器失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }

    private void showGamePanel() {
        JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
        frame.getContentPane().remove(this);
        GamePanel gamePanel = new GamePanel();
        frame.getContentPane().add(gamePanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        gamePanel.requestFocus();
    }
}


